/**
** ProcessRender.js file renders the flow diagram for the Process
** 
** Author: Mallikarjun
** Created on : 03/27/2002
**/

/**
**  ProcessRender constructor
**  arrow is  a Image object 
**  vline is a Image object
**  hline is a Image object
**/

function ProcessRender(window, process, divName, left, top, width){
    //alert ( "in process render cons " + window + " " + process+ " " + divName + " "+ left + " " + top);
	this.window = window;
	this.process = process;
	this.divName = divName;
	this.bgColor = bgColor;
	this.styleSheet = NArial;
	this.divStyle = BArial;

	this.left = left;
	this.top = top;
	this.width = width;
	this.arrow = arrow;
	this.body = "";
}

//alert ( "in process render");

if(navigator.appName.indexOf("Microsoft") != -1){

	ProcessRender.prototype.init = function(){
           	var d = this.window.document;
	     	d.writeln('<div ID="'+this.divName+'"');
		d.write(' STYLE="position:absolute;');
		if(this.left)d.write('left:'+this.left+';');
		if(this.top)d.write('top:'+this.top+';');
		if(this.width)d.write('width:'+this.width+';');
		d.write('" ');
		d.write('>');

	    	this.body += '<TABLE ALIGN="left" BORDER="0" ';
		this.body += 'CELLSPACING="0"  CELLPADDING="0" WIDTH="120">';
		this.body += '<TR ALIGN="left" VALIGN="middle">';
		this.body += '<TD ALIGN="center" VALIGN="middle">';
		this.body += '<FONT class="LArial">'
		this.body += this.process.getName();
		this.body += '</FONT></TD><TR>';

		var steps = this.process.getSteps();
		//alert ("process steps count " + steps.length);

            for(i=0; i<steps.length; i++){
			this.setBody(steps[i], i);
		}
		this.body += '</TABLE>';
		d.writeln(this.body);
		d.writeln('</div>');


	     	this.element = d.all[this.divName];
	     	this.style = this.element.style;

            for(var j=0; j<steps.length; j++){
			this.locationRender(steps[j],j)
		}

		this.imageDivision("downRender", down);
		this.imageDivision("upRender", up);
	}

	ProcessRender.prototype.setBody = function(pstep, i){
		this.body += '<TR ALIGN="Left" VALIGN="middle">';
		this.body += '<TD ALIGN="center" VALIGN="middle" >';
		this.body += '<IMG SRC="'+this.arrow.src+'" BORDER="0"';
		this.body += ' ALT="Click to Modify the '+pstep.getName()+'">';
		this.body += '</TD><TR>';
		this.body += '<TD ALIGN="center" VALIGN="middle" bgcolor=';
		this.body += '"'+this.bgColor+'" alt="">';
		this.body += '<A class="highHref" href="'+pstep.getHref()+'"';
		this.body += ' onMouseover="pr.showLocationDiv('+i+')">';
		this.body += '<FONT class='+ this.styleSheet+'>';
		this.body += pstep.getName()+'</FONT></A></TD></TR>';
		//alert("this.body"+this.body);
		//this.element.innerHTML= this.body;
	}

	ProcessRender.prototype.calculateOffset = function(idItem, index){
		var totalOffset = 0;
		var count = 0;
		var item = idItem.offsetHeight;

		for(var i=0; i<(index+1); i++){
			totalOffset += item;
		}
		/*do{
			
			totalOffset += eval('item.'+offsetName);
			item = eval('item.offsetParent');
			alert("This is check = "+ (count++) + "; total = "+ totalOffset);
		}while(item != null)
		*/
		
		return totalOffset;
	}

	ProcessRender.prototype.showLocationDiv = function(index){
		var d = this.window.document;
		var e = this.window.event;
		var proName = this.process.getStep(index).getName();
		
		var pstep = this.process.getSteps();
		var temp_proName = "";
		var tDivName = proName + index;
		//alert ( tDivName );

		//var style = d.all[proName].style;
		//alert (d.all[tDivName].style);
		var style = d.all[tDivName].style;
		//alert (d.all["downRender"].style);
		var imageStyle = d.all["downRender"].style;
		
		imageStyle.visibility = "visible";
		imageStyle.pixelLeft = this.style.pixelLeft+120;
		imageStyle.pixelTop = this.style.pixelTop+(index+1)*57; //e.clientY;
		//alert ( d.all["upRender"].style);
		d.all["upRender"].style.visibility = "hidden";

		for(var i=0; i<pstep.length; i++){
			temp_proName = pstep[i].getName();
			//alert ( "in for " + i +" " + (temp_proName == proName) );
			if(temp_proName == proName){
			     style.visibility = "visible";
			     style.left = imageStyle.pixelLeft+imageStyle.pixelWidth;
			     //style.top = e.clientY+63;
			     style.top = imageStyle.pixelTop+63;
			}else{
			    //alert (d.all[temp_proName+i].style);
				d.all[temp_proName+i].style.visibility = "hidden";
			}

			for(var j=0; j<pstep[i].products.getLocations().length; j++){
				d.all[temp_proName+i+"Lot"+j].style.visibility = "hidden";
			}
		}
		e.cancelBubble = true;
	}

	ProcessRender.prototype.showLotDiv = function(parentName, index, len){
		var d = this.window.document;
		var e = this.window.event;
		var org_name = parentName+"Lot"+index;
		var pstep = this.process.getSteps();
		//alert( "parentName " +parentName );
		//alert("org_name " + org_name );


		var temp_Divname = "";
		//alert ( d.all[org_name].style );
		//alert (org_name )
		var style = d.all[org_name].style;
		var downStyle = d.all["downRender"].style;
		var upStyle = d.all["upRender"].style;
		//alert ("ok "+ d.all[parentName].style);
		var parentStyle = d.all[parentName].style;

		//upStyle.visibility = "visible"
		upStyle.pixelLeft = this.style.pixelLeft+100+downStyle.pixelWidth+130;
		//upStyle.pixelTop = parentStyle.pixelTop+(index*12)-30; //e.clientY-83;

		//alert ( "bef for");
		for(var j=0; j<pstep.length; j++){
			var temp_Name = pstep[j].getName()+j;
			//alert ( "temp_Name " + temp_Name);
			for(var i=0; i<pstep[j].products.getLocations().length; i++){
				temp_Divname = temp_Name+"Lot"+i;
				//alert ("temp_Divname" + temp_Divname);
				if(temp_Divname == org_name){
					upStyle.pixelTop = parentStyle.pixelTop+(index*15)-50;; 
					upStyle.visibility = "visible";

					style.left = upStyle.pixelLeft+upStyle.pixelWidth;
					style.top = upStyle.pixelTop;
					style.visibility = "visible";

				}else{
						d.all[temp_Divname].style.visibility = "hidden";
				}
			}
		}
		e.cancelBubble = true;
	}


	ProcessRender.prototype.locationRender = function(pstep,index){
		var locations = pstep.products.getLocations();
		var divName = pstep.getName();
		var body = this.locationBodyCreator((divName+" Locations"), locations, divName+index);
		//alert ("*****"+body )
		this.divCreation(divName+index, body, "#dadcba");
		for(var k=0; k<locations.length; k++){
			this.lotRender(locations[k], k, divName+index);
		}
	}

	ProcessRender.prototype.lotRender = function(location, locIndex, parentDivName){
		var lots = location.getLots();
		var body = this.lotBodyCreator((location.getName()+" Lots"), lots);
		this.divCreation((parentDivName+"Lot"+locIndex), body, "#dee1e2");
	}

	ProcessRender.prototype.divCreation = function(div, body, color){
		var d = this.window.document; 
            d.writeln('<div ID="'+div+'" ');
		d.writeln(' style="Z-INDEX: 1; LEFT: 400px; VISIBILITY: hidden;');
		//d.writeln(' WIDTH: 146px; HEIGHT: 208px; POSITION: absolute;');
		d.writeln('POSITION:absolute;');
		d.writeln('TOP: 96px; BACKGROUND-COLOR:');
		d.write(color+';">')
		d.writeln(body);
		d.writeln('</div>');
	}

	ProcessRender.prototype.locationBodyCreator = function(heading, renderObj, funName){

		var body = this.headingCreator(heading);

		for(var k=0; k<renderObj.length; k++){
			body += '<TR ALIGN="center" VALIGN="middle">';
			body += '<TD ALIGN="center" VALIGN="middle" >';

			body += '<A class="highHref" href="';
			body += renderObj[k].getHref()+'"';
			body += ' onMouseover="pr.showLotDiv('+"'"+funName+"', "+k;
			body += ', '+ renderObj.length +')">';
			body += '<FONT class="'+ this.divStyle+'">'
			body += renderObj[k].getName();
			body += '</FONT></A></TD><TR>';
		}

		body += '</TABLE>';
		//alert("Location Body = " + body);
		return body;
	}

	ProcessRender.prototype.lotBodyCreator = function(heading, renderObj){

		var body = this.headingCreator(heading);
		for(var i=0; i<renderObj.length; i++){
			body += '<TR ALIGN="center" VALIGN="middle">';
			body += '<TD ALIGN="center" VALIGN="middle" >';
			body += '<A class="highHref" href="';
			body += renderObj[i].getHref()+'">';
			body += '<FONT class="'+ this.divStyle+'">'
			body += renderObj[i].getName();
			body += '</FONT></A></TD><TR>';
		}

		body += '</TABLE>';
		//alert("Lot Body = " + body);
		return body;
	}

	ProcessRender.prototype.headingCreator = function(heading){

		var body = "";
		body = '<TABLE ALIGN="center" VALIGN="middle" BORDER="0" ';
	 	body += 'CELLSPACING="0"  CELLPADDING="2" >';
		body += '<TR ALIGN="center" VALIGN="middle">';
		body += '<TD ALIGN="center" VALIGN="middle" bgcolor="';
		body += this.bgColor+'">';
		body += '<FONT class='+ this.styleSheet+'>'
		body += heading;
		body += '</FONT></TD><TR>';

		return body;
	}

	ProcessRender.prototype.imageDivision = function(name, img){
		var d = this.window.document; 
		var body ="";
            d.writeln('<div ID="'+name+'" ');
		d.writeln(' style="LEFT: 400px; VISIBILITY: hidden;');
		d.writeln(' WIDTH: 117px; HEIGHT: 80px; POSITION: absolute;');
		d.writeln('POSITION:absolute;');
		d.writeln('TOP: 96px; BACKGROUND-COLOR: #ffffff;">')

		body = '<TABLE ALIGN="center" VALIGN="middle" BORDER="0" ';
 		body += 'CELLSPACING="0"  CELLPADDING="0" >';

		body += '<TR ALIGN="center" VALIGN="middle">';
		body += '<TD ALIGN="center" VALIGN="middle" >';
		body += '<IMG SRC="'+img.src+'" BORDER="0" >';
		body += '</TD><TR>';
		body += '</TABLE>';

		d.writeln(body);
		d.writeln('</div>');				
	}
}

if(navigator.appName.indexOf("Navigator") != -1){
}
